<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/1/15
 * Time: 23:05
 */
namespace app\common\library\task;
use think\Config;
use think\exception\ValidateException;


/**
 * 税率计算
 */
class RateService
{
    /**
     * @var object 操作句柄
     */
    public static $handler;
    /**
     * @var array 缓存的实例
     */
    public static $instance = [];
    /**
     * 连接驱动
     * @access public
     * @param  array       $options 配置数组
     * @param  bool|string $name    强制重新连接
     * @return BaseRateService
     */
    public static function connect(array $options = [], $name = false)
    {
        $type = !empty($options['type']) ? $options['type'] : 'RateService';
        if (false === $name) {
            $name = md5(serialize($options));
        }
        if (true === $name || !isset(self::$instance[$name])) {
            $class = '\\app\\common\\library\\task\\' . ucwords($type);

            if (true === $name) {
                return new $class($options);
            }

            self::$instance[$name] = new $class($options);
        }

        return self::$instance[$name];
    }

    /**
     * 自动初始化
     * @access public
     * @param  array $options 配置数组
     * @return BaseRateService
     */
    public static function init(array $options = [])
    {

        if (is_null(self::$handler)) {
            if (empty($options)) {
                $options = Config::get('area_rate');
            }

            self::$handler = self::connect($options);
        }
        return self::$handler;
    }

    /**
     * 调用税务方法
     * @access public
     * @param  array $options 配置数组
     * @return BaseRateService
     */
    public static function instance(array $options = []) {
        return self::init($options);
    }

}